package com.example.booksearch;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hhdd.messi.Naver;
import com.hhdd.messi.event.NaverEventListener;
import com.hhdd.messi.naver.object.search.BookObject;

public class MainActivity extends Activity implements NaverEventListener.OnBookListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final Naver open_api = new Naver();
        open_api.setBookListener(this);
        open_api.setSearchKey("5be33141055ee2769497d4b01b0da9a8");
        
        Button btn = (Button) findViewById(R.id.bt1);
        btn.setOnClickListener(new OnClickListener(){
        	@Override
        	public void onClick(View v) {
        		EditText editText = (EditText)findViewById(R.id.et1);
        		open_api.BookSearch(editText.getText().toString());
        	}
        });
        							
    }

	@Override
	public void onFaultResult(int arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResult(final ArrayList<BookObject> arg0) {
		// TODO Auto-generated method stub
		ListView listView = (ListView) findViewById(R.id.lv1);
		ListAdapter b1_adapter = new ListAdapter(this, R.layout.item_row, arg0);
		listView.setAdapter(b1_adapter);
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				BookObject obj = arg0.get(position);
				Intent page = new Intent(MainActivity.this, BookDetailActivity.class);
				page.putExtra("title", obj.getTitle());
				page.putExtra("author", obj.getAuthor());
				page.putExtra("imageurl", obj.getImageUrl());
				page.putExtra("price", obj.getPrice());
				page.putExtra("discount", obj.getDiscount());
				startActivity(page);
			}
			
		});
	}
	
	public class ListAdapter extends ArrayAdapter<BookObject>{
    	public ArrayList<BookObject> items;
    	
    	public ListAdapter(Context context, int textViewResourceId, ArrayList<BookObject> items) {
            super(context, textViewResourceId, items);
    		this.items = items;
    	}
    	
    	@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.item_row, null);
            }
            
            BookObject Info = items.get(position);
            
            if (Info != null) {
            	ImageView iv1 = (ImageView) v.findViewById(R.id.iv1);
            	TextView tv1 = (TextView) v.findViewById(R.id.tv1);
            	TextView tv2 = (TextView) v.findViewById(R.id.tv2);
            	Info.BindImage(iv1);
            	tv1.setText(Info.getTitle());
            	tv2.setText(Info.getDescription());
            }
            return v;
        }
    	
    }
}
